package com.kifas.controller;

import java.util.ArrayList;
import java.util.List;

import javax.ws.rs.Consumes;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.PUT;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.UriInfo;
import javax.xml.bind.JAXBElement;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import com.kifas.entity.BarangEntity;
import com.kifas.entity.JenisBarangEntity;
import com.kifas.entity.SatuanEntity;
import com.kifas.model.xml.BarangModel;
import com.kifas.model.xml.JenisBarangModel;
import com.kifas.model.xml.PersistStatus;
import com.kifas.model.xml.SatuanModel;
import com.kifas.service.EntityServiceManager;

@Component
@Scope("prototype")
@Path("/Barangs")
public class BarangController {

	private static Logger log = Logger.getLogger(BarangController.class);

	@Context
	UriInfo uriInfo;

	@Autowired
	private EntityServiceManager entityManager;

	@GET
	@Produces(MediaType.TEXT_XML)
	public List<BarangModel> getBarangs() {
		List<BarangEntity> listEntityBarangs = entityManager.getBarangs();
		List<BarangModel> barangs = new ArrayList<BarangModel>();
		for (BarangEntity barangEntity : listEntityBarangs) {
			JenisBarangEntity jenisBarang = barangEntity.getJenisBarang();
			JenisBarangModel jenis = new JenisBarangModel(jenisBarang.getIdJenisBarang(), jenisBarang.getJenis(), jenisBarang.getCreatedBy());
			SatuanEntity satuanEntity = barangEntity.getSatuan();
			SatuanModel satuan = new SatuanModel(satuanEntity.getIdSatuan(), satuanEntity.getSatuan(), satuanEntity.getCreatedBy());
			BarangModel model = new BarangModel(barangEntity.getIdBarang(), barangEntity.getKdBarang(), jenis, satuan, barangEntity.getNama(),
					barangEntity.getHargaBeli(), barangEntity.getHargaJual(), barangEntity.getCreatedBy());
			model.setStokMaksimal(barangEntity.getStokMaksimal());
			model.setStokMinimal(barangEntity.getStokMinimal());
			barangs.add(model);
		}
		return barangs;
	}

	@GET
	@Path("{id}")
	@Produces(MediaType.TEXT_XML)
	public BarangModel getBarang(@PathParam("id") Long id) {
		BarangEntity barangEntity = entityManager.getBarang(id);
		JenisBarangEntity jenisBarang = barangEntity.getJenisBarang();
		JenisBarangModel jenis = new JenisBarangModel(jenisBarang.getIdJenisBarang(), jenisBarang.getJenis(), jenisBarang.getCreatedBy());
		SatuanEntity satuanEntity = barangEntity.getSatuan();
		SatuanModel satuan = new SatuanModel(satuanEntity.getIdSatuan(), satuanEntity.getSatuan(), satuanEntity.getCreatedBy());
		return new BarangModel(barangEntity.getIdBarang(), barangEntity.getKdBarang(), jenis, satuan, barangEntity.getNama(), barangEntity.getHargaBeli(),
				barangEntity.getHargaJual(), barangEntity.getCreatedBy());
	}

	@POST
	@Produces(MediaType.APPLICATION_XML)
	@Consumes(MediaType.APPLICATION_XML)
	public PersistStatus insert(JAXBElement<BarangModel> xml) {
		BarangModel model = xml.getValue();
		JenisBarangEntity jenisBarang = entityManager.getJenisBarang(model.getJenisBarang().getIdJenisBarang());
		SatuanEntity satuan = entityManager.getSatuan(model.getSatuan().getIdSatuan());
		BarangEntity barang = new BarangEntity(null, model.getKdBarang(), jenisBarang, satuan, model.getNamaBarang(), model.getHargaBeli(),
				model.getHargaJual(), model.getStokMinimal(), model.getStokMaksimal(), model.getCreatedBy());
		try {
			entityManager.persist(barang);
		} catch (Exception e) {
			log.error("can not persist barang entity", e);
			return new PersistStatus("BarangEntity", "failed", "can not persist barang entity");
		}
		return new PersistStatus("BarangEntity", "success", "success save barang data...");
	}

	@PUT
	@Consumes(MediaType.APPLICATION_XML)
	@Produces(MediaType.APPLICATION_XML)
	public PersistStatus update(JAXBElement<BarangModel> xml) {
		BarangModel model = xml.getValue();
		BarangEntity barang = entityManager.getBarang(model.getIdBarang());
		JenisBarangEntity jenisBarang = entityManager.getJenisBarang(model.getJenisBarang().getIdJenisBarang());
		SatuanEntity satuan = entityManager.getSatuan(model.getSatuan().getIdSatuan());
		if (barang == null || jenisBarang == null || satuan == null) {
			return new PersistStatus("BarangEntity", "failed", "barang not exist in database");
		}
		try {
			barang.setSatuan(satuan);
			barang.setJenisBarang(jenisBarang);
			barang.setStokMaksimal(model.getStokMaksimal());
			barang.setStokMinimal(model.getStokMinimal());
			barang.setNama(model.getNamaBarang());
			barang.setKdBarang(model.getKdBarang());
			barang.setHargaBeli(model.getHargaBeli());
			barang.setHargaJual(model.getHargaJual());
			entityManager.update(barang);
		} catch (Exception e) {
			return new PersistStatus("BarangEntity", "failed", "can not update barang");
		}
		return new PersistStatus("BarangEntity", "success");
	}

}
